package mylittleinvestigations.CaseInformation;

import java.io.IOException;
import java.util.*;
import mylittleinvestigations.FieldCutscene;

public class FieldCutsceneManager
{
    private HashMap<String, FieldCutscene> idToCutsceneHashMap;
    private HashMap<FieldCutscene, Boolean> cutsceneToOriginalEnabledStateHashMap;

    public FieldCutsceneManager()
    {
        this.idToCutsceneHashMap = new HashMap<String, FieldCutscene>();
    }

    public void AddCutscene(String id, FieldCutscene cutscene)
    {
        this.idToCutsceneHashMap.put(id, cutscene);
    }

    public FieldCutscene GetCutsceneFromId(String id)
    {
        return this.idToCutsceneHashMap.get(id);
    }
    
    public void Reset()
    {
        for (FieldCutscene cutscene : this.idToCutsceneHashMap.values())
        {
            cutscene.setIsEnabled(this.cutsceneToOriginalEnabledStateHashMap.get(cutscene));
            cutscene.setIsFinished(false);
            cutscene.setHasCompleted(false);
            cutscene.Reset();
        }
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("FieldCutsceneManager");
        
        writer.StartElement("IdToCutsceneHashMap");
        String[] ids = this.idToCutsceneHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            this.idToCutsceneHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.EndElement();
    }

    FieldCutsceneManager(CaseXmlReader reader)
    {
        this.idToCutsceneHashMap = new HashMap<String, FieldCutscene>();
        this.cutsceneToOriginalEnabledStateHashMap = new HashMap<FieldCutscene, Boolean>();
        
        reader.StartElement("FieldCutsceneManager");
        
        reader.StartElement("IdToCutsceneHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            FieldCutscene cutscene = new FieldCutscene(reader);
            this.idToCutsceneHashMap.put(id, cutscene);
            this.cutsceneToOriginalEnabledStateHashMap.put(cutscene, cutscene.getIsEnabled());
        }
        
        reader.EndElement();
        
        reader.EndElement();
    }
}
